import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:demo/event/event_setting.dart';
import 'package:demo/views/friend/friend_details_controller.dart';
import 'package:demo/views/friend/friend_remark_view.dart';
import 'package:demo/views/friend/friend_setting_view.dart';
import 'package:demo/tools/tools_enum.dart';
import 'package:demo/tools/tools_route.dart';
import 'package:demo/tools/tools_sqlite.dart';
import 'package:demo/component/component_action.dart';
import 'package:demo/component/component_common.dart';
import 'package:demo/component/component_image.dart';
import 'package:demo/component/component_line.dart';

// 好友详情
class FriendDetailsView extends GetView<FriendDetailsController> {
  // 路由地址
  static const String routeName = '/friend_details';
  const FriendDetailsView({super.key});

  @override
  Widget build(BuildContext context) {
    Get.lazyPut(() => FriendDetailsController());
    return GetBuilder<FriendDetailsController>(
      builder: (builder) {
        ChatFriend chatFriend = controller.refreshData;
        return Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: AppBar(
            title: const Text('好友详情'),
            actions: [
              ComponentAction(
                enable: chatFriend.userId != controller.localUser.userId,
                icon: const Icon(Icons.more_horiz),
                onTap: () {
                  Get.toNamed(
                    FriendSettingView.routeName,
                    arguments: chatFriend,
                  );
                },
              ),
            ],
          ),
          body: Column(
            children: [
              _buildHeader(chatFriend),
              ComponentCommon.border(
                enable: chatFriend.userId != controller.localUser.userId,
              ),
              ComponentLineCenter(
                '好友备注',
                enable: chatFriend.userId != controller.localUser.userId,
                divider: false,
                onTap: () {
                  Get.toNamed(FriendRemarkView.routeName);
                },
              ),
              ComponentCommon.border(),
              ComponentLineCenter(
                '发起聊天',
                divider: false,
                onTap: () {
                  ToolsRoute().chatPage(
                    chatId: chatFriend.userId,
                    nickname: chatFriend.nickname,
                    portrait: chatFriend.portrait,
                    remark: chatFriend.remark,
                    chatTalk: ChatTalk.friend,
                  );
                },
              ),
              ComponentCommon.border(),
              ComponentLineCenter(
                '清空消息',
                divider: false,
                color: Colors.red,
                onTap: () {
                  _clearHis(context);
                },
              ),
              ComponentCommon.border(),
            ],
          ),
        );
      },
    );
  }

  // 顶部头像
  _buildHeader(ChatFriend chatFriend) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Padding(
            padding: const EdgeInsets.only(right: 10),
            child: GestureDetector(
              child: ComponentCommon.showAvatar(
                chatFriend.portrait,
                size: 65,
              ),
              onTap: () {
                Get.to(
                  ShowImage(chatFriend.portrait, ImageType.asset),
                  transition: Transition.topLevel,
                );
              },
            ),
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  chatFriend.remark.isNotEmpty
                      ? chatFriend.remark
                      : chatFriend.nickname,
                  style: const TextStyle(
                    fontWeight: FontWeight.bold,
                  ),
                  overflow: TextOverflow.clip,
                ),
                Text('ID：${chatFriend.userNo}'),
                Text(
                  '昵称：${chatFriend.nickname}',
                  overflow: TextOverflow.visible,
                ),
                Text(
                  '签名：${chatFriend.intro}',
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // 清空历史
  _clearHis(BuildContext context) {
    showCupertinoDialog(
      context: context,
      builder: (BuildContext context) {
        return CupertinoAlertDialog(
          content: const Text(
            '确认清空消息吗？',
            style: TextStyle(
              fontSize: 16,
            ),
          ),
          actions: [
            CupertinoDialogAction(
              child: const Text('取消'),
              onPressed: () {
                Get.back();
              },
            ),
            CupertinoDialogAction(
              child: const Text('确认'),
              onPressed: () {
                Get.back();
                EventSetting().handle(
                  SettingModel(
                    SettingType.clear,
                    primary: controller.refreshData.userId,
                  ),
                );
                // 提醒
                EasyLoading.showToast('清空成功');
              },
            ),
          ],
        );
      },
    );
  }
}
